package com.itheima.Demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Demo比较器自定义类型比较 {
    public static void main(String[] args) {
        //创建集合;
        ArrayList<Student> list  = new ArrayList<>();
        //添加元素;
        list.add(new Student("张晗",18));
        list.add(new Student("拜清清",19));
        list.add(new Student("张梓墨",1));
        System.out.println(list);
        //排序
        //要对学生进行排序,但是没有排序的规则
        //Collections.sort(list);
        //可以自己传入比较器对象进行排序
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge()-o1.getAge();
            }
        });
        System.out.println(list);

    }
}
